<?php

/**
 * Created by PhpStorm.
 * User: 彪
 * Date: 2016-5-29
 * Time: 17:26
 */
class Pay extends Base_Controller
{
    public function __construct()
    {
        parent::__construct();

        $this->load->model('transaction_model');
        $this->load->model('track_model');
        $this->load->model('article_model');
        $this->load->model('ziying_model');

    }

    public function addorder()
    {
        $data= $_GET;
//        $data['persons'] = 1;
//        $data['startDay'] = '2016-01-05';
//        $data['stopDay'] = '2016-01-05';
//        $data['uid'] = 95;
        if(empty($data['goodsId']) || empty($data['persons']) || empty($data['startDay'])  || empty($data['type']) || empty($data['uid']))
        {
            $this->error('参数不全');
        }

        switch ($data['type'])
        {
            case 'TRACK':
                $model = 'track_model';
                $good = $this->$model->getbyid($data['goodsId']);
                $priceTrack = $good['priceFood'] + $good['priceTraffic'] + $good['priceTicket'] + $good['priceOther'];
                $moneyGoods =  ($good['priceService'] + $priceTrack * $data['persons'])*1.1;
                break;
            case 'ACTIVITY':
                $model = 'article_model';
                $good = $this->$model->getbyid($data['goodsId']);
                $moneyGoods =  $good['priceService'] *  $data['persons'] * 1.1;
                break;
            case 'ZIYING':
                $model = 'ziying_model';
                $good = $this->$model->getbyid($data['goodsId']);

                $pdetail =  $good['priceDetail'];

                $pDays=array();
                if(!empty($pdetail)){
                    $pdetail=explode(',',$pdetail);

                    foreach( $pdetail as $v){
                        if(!empty($v)){
                            $p=explode(':',$v);
                            $p[0]=trim($p[0]);
                            $p[1]=trim($p[1]);
                            $pDays[$p[0]]=$p[1];
                        }
                    }
                    $today=strtotime(date("Y-m-d"));
                    foreach ($pDays as $d=>$p){
                        $dtime=strtotime($d);
                        if($dtime<$today)
                            unset($pDays[$d]);
                    }
                }
                $price = $pDays[$data['startDay']];
                $moneyGoods = $price * $data['persons'] * 100;
                break;
            default:
                $this->error('类型错误');
        }
//        $good = $this->$model->getbyid($data['goodsId']);
        if ($data['type'] == 'ACTIVITY' && empty($data['stopDay']))
            $data['stopDay'] = $data['startDay'];



        // order
        $orderData = array(
            'status'  => 0,
            'created' => time(),
            'changed' => time(),
        );

        $orderData['title']       =$good['name'];
        $orderData['uid']         = $data['uid'];
        $orderData['shopId']      = $good['uid'];
        $orderData['countsGoods'] = 1;
        $orderData['countsQuantity'] = $data['persons'];
        $orderData['startDay']    = $data['startDay'];
        $orderData['stopDay']     = $data['stopDay'];
        $orderData['goodsId']     = $data['goodsId'];
        $orderData['goodsType']   = $data['type'];
        $orderData['persons']     = $data['persons'];

        // 金额信息
//        $moneyGoods  = $goods->getMoney($startDay, $stopDay, $persons);
        $orderData['moneyCoin']    = 0;
        $orderData['moneyTicket']  = 0;
        $orderData['moneyDeliver'] = 0;
        $orderData['moneyGoods']   = $moneyGoods;
        $orderData['moneyTotal']   = $moneyGoods;
        $orderData['commentsUser'] = '';
        $orderData['orderType']    = 'NORMAL';
        $orderData['tuanId']       = 0;
        $r = $this->transaction_model->addOrder($orderData);

        if($r)
        {
            $this->success(array('transId'=>$r));
        }else{
            $this->error('订单失败');
        }

    }


    public function alipay()
    {

        if (empty($_GET['transId']) || empty($_GET['return_url']) ||  empty($_GET['show_url']) )
        {
            $this->error('参数不全');
        }
        $data = $this->transaction_model->detail($_GET['transId']);
        $parameter = array(
            "out_trade_no" =>$data['out_trade_no'],
            "total_fee"    => $data['total_fee'] / 100,   // 元
            "body"         => $data['body'],
            "notify_url"   => 'http://api.waya.com/index.php/pay/isalipay',
            "return_url"   => $_GET['return_url'],
            "show_url"     =>  $_GET['show_url'],
            "product"      =>$data['body'],
        );
        $this->load->library('ali/Alipay');
        $this->alipay->zhifu($parameter);
    }

    public function isalipay()
    {
        $this->load->library('ali/Alipay');
        $re =  $this->alipay->return_url();

        if ($re === false)
        {
            return FALSE;
        }

        $orderNo       =  $re['out_trade_no'];
//        $orderNo  = '201605112155000155';
        $r['transactionId'] = empty($re['transaction_id'])?null:$re['transaction_id'];
        $r['merchantId']    = NULL;
        $r['resultCode']    = 'SUCC';//$data['trade_status'];
        $r['returnCode']    = 'SUCC';//$data['trade_status'];
        $trans = $this->transaction_model->detailBy($orderNo);
        if ($trans)
        {
            $r['platform'] = 'ALIPAY';
            $this->transaction_model->notify_paid ($trans['id'], $r);
            return true;
        }
        else
        {
            return false;
        }
    }

    public function weixinpay()
    {

    }

}